﻿// By Conmajia<conmajia@gmail.com> on September 6th., 2012
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace CSharpGo
{
public class Bottle : Element, IVisible
{
    const int SIZE_FACTOR = 10;

    #region Variables
    int content = 0;
    int capacity = 0;
    Size size = Size.Empty;
    Rectangle bounds = Rectangle.Empty;

    Bitmap canvas;
    Graphics painter;
    bool dirty = true;

    #endregion

    #region Properties
    public int FreeSpace
    {
        get { return capacity - content; }
    }

    public int Content
    {
        get { return content; }
    }

    public int Capacity
    {
        get { return capacity; }
    }

    public bool IsEmpty
    {
        get { return content == 0; }
    }

    public bool IsFull
    {
        get { return content == capacity; }
    }
    #endregion

    #region Initiators
    public Bottle(int capacity)
        : this(capacity, 0)
    {

    }

    public Bottle(int capacity, int content)
    {
        if (capacity > 0)
        {
            this.capacity = capacity;
            if (content > -1 && content <= capacity)
                this.content = content;

            size.Width = 30;
            size.Height = SIZE_FACTOR * capacity;
            bounds.Size = size;
            canvas = new Bitmap(size.Width, size.Height);
            painter = Graphics.FromImage(canvas);
        }
    }
    #endregion

    #region Methods
    public void DropIn()
    {
        DropIn(capacity);
    }
    public void DropIn(int amount)
    {
        if (amount > 0)
        {
            content += amount;
            if (content > capacity)
                content = capacity;

            dirty = true;
        }
    }

    public void DropOut()
    {
        DropOut(capacity);
    }
    public void DropOut(int amount)
    {
        if (amount > 0 && amount < content)
        {
            content -= amount;
        }
        else
        {
            content = 0;
        }

        dirty = true;
    }
    #endregion

    #region IVisible
    public Rectangle Bounds
    {
        get { return bounds; }
    }

    public int X
    {
        get { return bounds.X; }
        set { bounds.X = value; }
    }

    public int Y
    {
        get { return bounds.Y; }
        set { bounds.Y = value; }
    }

    public Bitmap Draw()
    {
        if (dirty)
        {
            painter.Clear(Color.Transparent);

            // simple look bottle
            int contentHeight = (int)((float)bounds.Height * ((float)content / (float)capacity));

            if (contentHeight > 0)
            {
                using (Brush b = new LinearGradientBrush(
                    new Rectangle(
                        0,
                        bounds.Height - contentHeight - 1,
                        bounds.Width,
                        contentHeight
                        ),
                    Color.LightBlue,
                    Color.DarkBlue,
                    90))
                {
                    Rectangle contentRect = new Rectangle(
                        0,
                        bounds.Height - contentHeight,
                        bounds.Width,
                        contentHeight
                        );
                    painter.FillRectangle(b, contentRect);
                }
            }

            painter.DrawRectangle(
                Pens.Silver,
                0,
                0,
                bounds.Width - 1,
                bounds.Height - 1
                );

            string s = string.Format("{0}/{1}", content, capacity);
            painter.DrawString(
                s,
                SystemFonts.DefaultFont,
                Brushes.Black,
                2,
                1
                );
            painter.DrawString(
                s,
                SystemFonts.DefaultFont,
                Brushes.Black,
                1,
                2
                );
            painter.DrawString(
                s,
                SystemFonts.DefaultFont,
                Brushes.Black,
                2,
                3
                );
            painter.DrawString(
                s,
                SystemFonts.DefaultFont,
                Brushes.Black,
                3,
                2
                );
            painter.DrawString(
                s,
                SystemFonts.DefaultFont,
                Brushes.White,
                2,
                2
                );

            dirty = false;
        }

        return canvas;
    }
    #endregion

    #region Elemenet
    public override Type Type
    {
        get { return typeof(Bottle); }
    }
    #endregion

}
}
